package com.fucking.great.context.handler;

import com.fucking.great.context.TxnContext;
import com.fucking.great.context.exception.TxnRuntimeException;

import java.util.concurrent.Semaphore;

/**
 * 抽象的装饰者
 */
public  class SemaphoreHandlerDecorator extends AbstractHandlerDecorator{

    private final Semaphore semaphore = new Semaphore(10000);

    public SemaphoreHandlerDecorator(GreatHandler concreteComponenter) {
        super(concreteComponenter);
    }

    @Override
    public Object doService(TxnContext text){
        Object ret = null;
        try {
            this.semaphore.acquire();
            ret = super.doService(text);
        } catch (InterruptedException e) {
            TxnRuntimeException.throwException("SYS_LIMIT_CURRENT",e.getMessage(),e);
        }finally {
            this.semaphore.release();
        }
        return ret;
    }

}
